package com.example.city_defense.sprites;

import android.graphics.Matrix;
import android.util.Log;

import com.badlogic.androidgames.framework.Pixmap;
import com.example.city_defense.Asset;
import com.example.city_defense.GameScreen;
import com.example.city_defense.sprites.Vehicle.VehicleState;

public class Tower {

	public int x;
	public int y;
	public Pixmap image;
	public float direction;
	public int base;
	public int type;
	private float RR;
	public float tickTime;
	public float rechargeTime;
	public Vehicle target;
	GameScreen gameScreen;
	Matrix maxtrix = new Matrix();

	public Tower(GameScreen gameScreen, int x, int y, int type, int base) {
		this.x = x;
		this.y = y;

		this.type = type;
		this.load();
		this.base = base;
		this.direction = 0;
		this.target = null;
		this.tickTime = rechargeTime;
		this.gameScreen = gameScreen;

	}

	public float getRR() {
		return this.RR;
	}

	public void update(float deltaTime) {
		tickTime += deltaTime;
		// target != null
		// conditional target
		if (this.target != null) {
			// conditional distance
			float distance = (float) (Math.pow(this.x - target.x, 2) + Math
					.pow(this.y - target.y, 2));
			//
			if (distance <= this.RR) {
				// Rotate Tower
				// this.rotate();
				// conditional Time
				if (tickTime >= rechargeTime
						&& target.state == VehicleState.Running) {
					Bullet bullet = new Bullet(this.x, this.y,/* Type */
					this.type, this.target);
					gameScreen.listBullet.add(bullet);
					this.tickTime = 0;
				} else if (target.state != VehicleState.Running) {
					this.target = null;
				}

			} else {
				target = null;
				tickTime = 0;
			}
		} else {
			for (Vehicle vehicle : gameScreen.listVehicle) {
				if (vehicle.state == VehicleState.Running) {
					float distance = (float) (Math.pow(this.x - vehicle.x, 2) + Math
							.pow(this.y - vehicle.y, 2));
					if (distance <= this.getRR()) {
						this.target = vehicle;
						// Phu thuoc vao tung loai xe
						break;
					}
				}// end check distance
			}// end for Vehicle
		}
	}

	private void load() {
		switch (this.type) {
		// Missle
		case 1:
			this.image = Asset.tower1;
			this.RR = 22500;
			this.rechargeTime = 2f;
			break;
		// Howitzer
		case 2:
			this.image = Asset.tower2;
			this.RR = 90000;
			this.rechargeTime = 3.8f;
			break;
		// Tebla
		case 3:
			this.image = Asset.tower3;
			this.RR = 10000;
			this.rechargeTime = 2f;
			break;
		// Laser
		case 4:
			this.image = Asset.tower4;
			this.RR = 22500;
			this.rechargeTime = 0.5f;
			break;
		// Oil Spray
		case 5:
			this.image = Asset.tower5;
			this.RR = 22500;
			this.rechargeTime = 3f;
			break;
		// AA Gun
		case 6:
			this.image = Asset.tower6;
			this.RR = 22500;
			this.rechargeTime = 0.4f;
			break;
		// GOO
		case 7:
			this.image = Asset.tower7;
			this.RR = 22500;
			this.rechargeTime = 2.3f;
			break;
		// Chain
		case 8:
			this.image = Asset.tower8;
			this.RR = 22500;
			this.rechargeTime = 2f;
			break;
		// Gauss
		case 9:
			this.image = Asset.tower9;
			this.RR = 40000;
			this.rechargeTime = 2f;
			break;
		// Nulificator
		case 10:
			this.image = Asset.tower10;
			this.RR = 22500;
			this.rechargeTime = 2f;
			break;
		}
	}

}
